<?php

class Zend_Controller_Action_Helper_Isloggedin extends Zend_Controller_Action_Helper_Abstract {

	/*
	 *   Action Helper
	 *   $var = $this->_helper->isloggedin(); // (ruft direct auf)
	 *   $var = $this->_helper->isloggedin->isLoggedin();
	 */

	/**
	 * @return int|null
	 */
	public function direct() {
		return $this->isloggedin();
	}

	/**
	 * prüft ob Session und kontrolliert ob User-DB-Record
	 * @return Application_Model_User $user | null
	 */
	public function isloggedin() {
		$auth = Zend_Auth::getInstance();
		if ($auth->hasIdentity()) {
			$userId = $auth->getIdentity();
			$userId = $userId[1];

			$user_mp = new Application_Model_UserMapper();
			$user = $user_mp->find($userId );
			$realName = $user->getRealName();
			if ($realName == "") {
				Zend_Auth::getInstance()->clearIdentity();
				throw new Exception('Applikationsfehler - kein User verhanden');
				//$view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');
				//$view._init
				//$view = $this->getResource('view');
				//$view->render('auth', 'login');
			} else {
				return $user;
			}

		} else {
			// nicht eingeloggt
			return null;
		}

		return null;
	}
}
